using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;
using System.Xml.Linq;
/// <summary>
///GlobalDataHandler 的摘要说明
/// </summary>
public class GlobalDataHandler
{
	public GlobalDataHandler()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public static bool addItem(string username)
    {
        try
        {
            XElement root = XElement.Load(HttpContext.Current.Server.MapPath("")+"\\userdata\\regTable.xml");
            Int32 lastId = Convert.ToInt32(root.Elements("user").Last());
            string tmpId = (lastId+1).ToString();
            root.Add(new XElement("user",new XElement("ID",tmpId),new XElement("username",username)));
            root.Attribute("maxID").Value = tmpId;
            root.Attribute("total").Value = (Convert.ToInt32(root.Attribute("total"))+1).ToString();
            root.Save(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public static bool delItemByUsername(string username)
    {
        try
        {
            XElement root = XElement.Load(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            root.LastNode.Remove();
            Int32 lastId = Convert.ToInt32(root.Elements("user").Last());
            root.Attribute("maxID").Value = lastId.ToString();
            root.Attribute("total").Value =  (Convert.ToInt32(root.Attribute("total")) - 1).ToString();
            root.Save(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public static bool delItemByID(string Id)
    {
        try
        {
            XElement root = XElement.Load(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            IEnumerable<XElement> element =
                from el in root.Elements()
                where el.Element("ID").Equals(new XElement("ID",Id))
                select el;
            element.Remove();
            root.Attribute("total").Value = (Convert.ToInt32(root.Attribute("total")) - 1).ToString();
            root.Save(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            return true;
        }
        catch (Exception ex)
        {
            LOG.writer(ex.ToString());
            return false;
        }
    }

    public static string getUsernameByID(string Id)
    {
        try
        {
            XElement root = XElement.Load(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            IEnumerable<XElement> element =
                from el in root.Elements()
                where el.Element("ID").Equals(new XElement("ID", Id))
                select el.Element("username");
            return element.ToString();
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

    public static string getIDByUsername(string username)
    {
        try
        {
            XElement root = XElement.Load(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
            IEnumerable<XElement> element =
                from el in root.Elements()
                where el.Element("username").Equals(new XElement("username", username))
                select el.Element("username");
            return element.ToString();
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

    public static string getMaxID()
    {
        XElement root = XElement.Load(HttpContext.Current.Server.MapPath("") + "\\userdata\\regTable.xml");
        return root.Attribute("maxID").Value;
    }
}
